package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"guo-group/cache"
	"guo-group/models"
	"guo-group/pkg"
	"strconv"
	"time"
)

type OrderController struct {
	beego.Controller
}
type PageData struct {
	Data  interface{}
	Count int64
}

// 登录
func (c *OrderController) Login() {
	username := c.GetString("username")
	password := c.GetString("password")
	if username == "" || password == "" {
		c.Data["json"] = Red{
			Code: 500,
			Data: nil,
			Msg:  "账号密码不能为空",
		}
		c.ServeJSON()
		return
	}
	var m models.User

	data := "data_1111"
	i, _ := cache.Rdb.Get(cache.Ctx, data).Int()
	if i >= 3 {
		c.Data["json"] = Red{
			Code: 500,
			Data: nil,
			Msg:  "错误三次禁止登录",
		}
		c.ServeJSON()
		return
	}
	err := models.Db.Where("username=?", username).First(&m).Error
	if err != nil {
		c.Data["json"] = Red{
			Code: 500,
			Data: nil,
			Msg:  "账号不存在",
		}
		c.ServeJSON()
		return
	}
	if m.Password != pkg.Mad5(password) {
		cache.Rdb.Incr(cache.Ctx, data)
		if i == 2 {
			cache.Rdb.Set(cache.Ctx, data, 3, time.Hour*3)
			c.Data["json"] = Red{
				Code: 500,
				Data: nil,
				Msg:  "密码错误三次封禁3小时",
			}
			c.ServeJSON()
			return
		}
		c.Data["json"] = Red{
			Code: 500,
			Data: nil,
			Msg:  "密码错误",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Red{
		Code: 200,
		Data: m,
		Msg:  "登陆成功",
	}
	c.ServeJSON()
	return
}

// 角色分配
func (c *OrderController) UserOrderAdd() {
	oid, _ := c.GetInt("oid")
	qid, _ := c.GetInt("qid")
	quan := models.OrderQuan{
		Oid: oid,
		Qid: qid,
	}
	err := models.Db.Create(&quan).Error
	if err != nil {
		c.Data["json"] = Red{
			Code: 500,
			Data: nil,
			Msg:  "添加失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Red{
			Code: 500,
			Data: quan,
			Msg:  "添加成功",
		}
		c.ServeJSON()
	}
}

// 用户角色
func (c *OrderController) UserOrder() {
	uid, _ := c.GetInt("uid")
	var m []models.Order
	err := models.Db.Table("orders").
		Joins("left join user_orders on user_orders.oid = orders.id").
		Where("user_orders.uid=?", uid).Debug().
		Find(&m).Error
	if err != nil {
		c.Data["json"] = Red{
			Code: 500,
			Data: m,
			Msg:  "失败",
		}
		c.ServeJSON()
	}
	c.Data["json"] = Red{
		Code: 200,
		Data: m,
		Msg:  "查询成功",
	}
	c.ServeJSON()
}

// 角色权限
func (c *OrderController) OrderQuan() {
	oid, _ := c.GetInt("oid")
	var m []models.Quan
	err := models.Db.Table("quans").
		Joins("left join order_quans on order_quans.qid=quans.id").
		Where("order_quans.oid=?", oid).Find(&m).Debug().Error
	if err != nil {
		c.Data["json"] = Red{
			Code: 500,
			Data: nil,
			Msg:  "查询失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Red{
			Code: 200,
			Data: m,
			Msg:  "查询成功",
		}
		c.ServeJSON()
	}
}

// 单号添加
func (c *OrderController) GongHans() {
	kuai := c.GetString("kuai")
	gid, _ := c.GetInt("gid")
	hid, _ := c.GetInt("hid")
	han := models.GongHan{
		Kuai: kuai,
		Gid:  gid,
		Hid:  hid,
	}
	err := models.Db.Create(&han).Error
	if err != nil {
		c.Data["json"] = Red{
			Code: 500,
			Data: nil,
			Msg:  "添加失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Red{
			Code: 200,
			Data: han,
			Msg:  "添加成功",
		}
		c.ServeJSON()
	}
}

// 公司添加
func (c *OrderController) GongAdd() {
	user := c.GetString("user")
	gong := models.Gong{
		Users: user,
	}
	err := models.Db.Create(&gong).Error
	if err != nil {
		c.Data["json"] = Red{
			Code: 500,
			Data: nil,
			Msg:  "添加失败",
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Red{
			Code: 200,
			Data: gong,
			Msg:  "添加成功",
		}
		c.ServeJSON()
	}
}

// 三表联查
func (c *OrderController) GongHan() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	i := (page - 1) * size
	data := "data_1111"
	var m []models.GongHaoData
	result, _ := cache.Rdb.Get(cache.Ctx, data).Result()
	if result == "" {
		models.Db.Table("gong_hans").
			Joins("left join gongs on gong_hans.gid = gongs.id").
			Joins("left join haos on gong_hans.hid = haos.id ").
			Select("gong_hans.id as Id", "gong_hans.kuai as Nade", "gongs.users as Gname", "haos.dan as Hname").
			Limit(size).Offset(i).Find(&m).Debug()
		marshal, _ := json.Marshal(&m)
		cache.Rdb.Set(cache.Ctx, data, marshal, time.Hour*1)
	} else {
		err := json.Unmarshal([]byte(result), &m)
		if err != nil {
			c.Data["json"] = Red{
				Code: 500,
				Data: nil,
				Msg:  "查询失败",
			}
			c.ServeJSON()
			return
		}
	}
	var count int64
	models.Db.Table("gong_hans").Count(&count)

	c.Data["json"] = Red{
		Code: 200,
		Data: PageData{
			Data:  m,
			Count: count,
		},
		Msg: "查询成功",
	}
	c.ServeJSON()
}

// 快递类型
func (c *OrderController) Han() {
	id, _ := c.GetInt("id")
	if id != 0 {
		c.Data["json"] = Red{
			Code: 500,
			Data: nil,
			Msg:  "用户id不能为空",
		}
		c.ServeJSON()
		return
	}
	var m models.Hao
	data := "date_2222" + strconv.Itoa(id)
	result, _ := cache.Rdb.Get(cache.Ctx, data).Result()
	if result == "" {
		models.Db.Model(&models.Hao{}).Find(&m)
		marshal, _ := json.Marshal(&m)
		cache.Rdb.Set(cache.Ctx, data, marshal, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(result), &m)
		if err != nil {
			c.Data["json"] = Red{
				Code: 500,
				Data: nil,
				Msg:  "查询失败",
			}
			c.ServeJSON()
			return
		} else {
			c.Data["json"] = Red{
				Code: 500,
				Data: m,
				Msg:  "查询成功",
			}
			c.ServeJSON()
			return
		}
	}
}
